package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.D;
import com.diboot.core.util.V;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Status;
import com.diboot.file.controller.BaseExcelFileController;
import com.diboot.file.excel.listener.ReadExcelListener;
import com.diboot.file.util.ExcelHelper;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.OperationCons;
import com.example.demo.dto.ProductionMaterialUsageDTO;
import com.example.demo.entity.ProcurementOrderItem;
import com.example.demo.entity.ProductionMaterialUsage;
import com.example.demo.excel.listener.ProcurementOrderItemImportListener;
import com.example.demo.excel.listener.ProductionMaterialUsageImportListener;
import com.example.demo.excel.model.ProcurementOrderItemImportModel;
import com.example.demo.excel.model.ProductionMaterialUsageExportModel;
import com.example.demo.excel.model.ProductionMaterialUsageImportModel;
import com.example.demo.service.ProcurementOrderItemService;
import com.example.demo.service.ProductionMaterialUsageService;
import com.example.demo.vo.ProductionMaterialUsageListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 项目文件 Excel处理Controller
 * @author MyName
 * @version 1.0
 * @date 2023-09-13
 * Copyright © MyCompany
 */
@RestController
@RequestMapping("/productionMaterialUsage/excel")
@BindPermission(name = "物料登记导入导出", code = "ProductionMaterialUsageExcel")
@Slf4j
public class ProductionMaterialUsageExcelController extends BaseExcelFileController {

    @Autowired
    private ProductionMaterialUsageService productionMaterialUsageService;

    /**
     * 预览数据
     *
     * @param file
     * @param params
     * @throws Exception
     */
    @BindPermission(name = "导入预览", code = OperationCons.CODE_IMPORT)
    @PostMapping("/preview")
    public JsonResult preview(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> params) throws Exception {
        return super.excelPreview(file, ProductionMaterialUsage.class, params);
    }

    /**
     * 导入预览保存
     *
     * @param params
     * @throws Exception
     */
    @BindPermission(name = "导入预览保存", code = OperationCons.CODE_IMPORT)
    @PostMapping("/previewSave")
    public JsonResult previewSave(@RequestParam Map<String, Object> params) throws Exception {
        return super.excelPreviewSave(params);
    }

    /**
     * 上传excel并保存
     *
     * @param file
     * @param params
     * @throws Exception
     */
    @BindPermission(name = "直接导入", code = OperationCons.CODE_IMPORT)
    @PostMapping("/upload")
    public JsonResult upload(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> params) throws Exception {
        return super.uploadExcelFile(file, ProductionMaterialUsage.class, params);
    }

    /**
     * 下载示例文件
     *
     * @param response
     * @throws Exception
     */
    @BindPermission(name = "导入物料登记示例文件下载", code = OperationCons.CODE_IMPORT)
    @GetMapping("/downloadExample")
    public void downloadExample(HttpServletResponse response) throws Exception{
        String fileName = "物料登记导入示例.xlsx";
        ExcelHelper.exportExcel(response, fileName, ProductionMaterialUsageImportModel.class, null);
    }

    /**
     * 项目文件列表导出
     *
     * @param response
     * @return
     * @throws Exception
     */
    @BindPermission(name = "导出列表文件", code = OperationCons.CODE_EXPORT)
    @GetMapping("/export")
    public JsonResult export(ProductionMaterialUsageDTO projectFileDto, @RequestParam(value = "columns", required = false) List<String> columns,
                             HttpServletResponse response) throws Exception{
        QueryWrapper<ProductionMaterialUsage> queryWrapper = super.buildQueryWrapperByQueryParams(projectFileDto);
        List<ProductionMaterialUsageListVO> productionMaterialUsageList = productionMaterialUsageService.getViewObjectList(queryWrapper, null, ProductionMaterialUsageListVO.class);
        if (V.isEmpty(productionMaterialUsageList)) {
            return new JsonResult(Status.FAIL_OPERATION, "物料登记列表为空，导出失败");
        }
        String fileName = "物料登记列表导出_" + D.today() + ".xlsx";
        List<ProductionMaterialUsageExportModel> dataList = this.entityList2ExcelList(productionMaterialUsageList);
        ExcelHelper.exportExcel(response, fileName, ProductionMaterialUsageExportModel.class, columns, dataList);
        return null;
    }

    /**
     * 实体列表转excel列表
     *
     * @param
     * @return
     */
    private List<ProductionMaterialUsageExportModel> entityList2ExcelList(List<ProductionMaterialUsageListVO> projectFileList) {
        if (V.isEmpty(projectFileList)) {
            return Collections.emptyList();
        }
        return BeanUtils.convertList(projectFileList, ProductionMaterialUsageExportModel.class);
    }

    @Override
    protected ReadExcelListener<ProductionMaterialUsageImportModel> getExcelDataListener() {
        return new ProductionMaterialUsageImportListener();
    }
}
